1689B - Mystic Permutation - CodeForces Solution


data structures greedy *900

Please click on ads to support us..

Python Code:

t=int(input())
while(t):
	n=int(input())
	L=input().split()
	if(n==1):
		print(-1)
	else:
		N,M=[],[]
		for i in range(n):
			L[i]=int(L[i])
			N.append(0)
			M.append(L[i])
		M.sort()
		x=[]
		for i in range(n):
			if(L[i]==M[i]):
				N[i]=1
				x.append(i)
		if(x==[]):
			for i in range(n):
				print(M[i],end=" ")
			print("")
		else:
			j=0
			l=len(x)
			while(x):
				if(j>=l):
					break
				if(x[j]==n-1):
					M[-1],M[-2]=M[-2],M[-1]
					N[-1]=0
					x=[]
				else:
					N[x[j]],N[x[j]+1]=0,0
					M[x[j]],M[x[j]+1]=M[x[j]+1],M[x[j]]
					if(j==l-1):
						j+=1
					else:
						if(x[j+1]-x[j]==1):
							j+=2
						else:
							j+=1
			for i in M:
				print(i,end=" ")
			print("")

	t-=1

C++ Code:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef map<int,int> mii;
typedef pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<long long> vl;
typedef map<long long,long long> mll;
typedef pair<long long,long long> pl;

#define F first
#define pb push_back
#define S second
#define er erase    
#define in insert
#define NOO cout<<"NO"<<nl;
#define YES cout<<"YES"<<nl;
#define NL cout<<endl;
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forin(i, n) for (int i = int(n-1); i >= 0; i--)
#define fast cin.tie(0);cout.tie(0);ios::sync_with_stdio(0)
#define test freopen("input.in","r",stdin); freopen("output.in","w",stdout)


const int N = 2e5 + 55;
// const ll mod = 1e9 + 7;
const ll mod = 998244353;
const char nl = '\n';

ll fact[N];
ll x,y,n,l,r,p,m,h,k,ans,ans2,opt,mid;
ll mx=INT_MIN,mn=INT_MAX,sm;
bool ok;
ll b[N],c[N],a[N];

void solve(){
	cin>>n;
	forn(i,n)cin>>a[i];
	forn(i,n)b[i]=a[i];
	sort(a,a+n);
	if(n==1){
		cout<<-1<<nl;
		return;
	}
	forn(i,n-1) if(a[i]==b[i])swap(a[i],a[i+1]);
	if(a[n-1]==b[n-1])swap(a[n-1],a[n-2]);
	forn(i,n)cout<<a[i]<<" ";
	cout<<nl;
}   

int main()  
{
    fast;
    #ifndef ONLINE_JUDGE
        test; 
    #endif

    int Tc=1;
    cin >> Tc;
    while(Tc--){
        solve();
    }   

    return 0;
}


Comments

Submit
0 Comments
More Questions

855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23